﻿using System.Diagnostics;
using System.Text;

namespace SeguradoraIA.Framework.Log
{
    public static class StackTracer
    {
        public static void GetStackInfo(LogData logData)
        {
            StackTrace stackTrace = new StackTrace(4, true);
            StackFrame stackFrame = stackTrace.GetFrame(0);

            logData.Method = stackFrame.GetMethod().ReflectedType + "." + stackFrame.GetMethod().Name;
            logData.ParametersDeclaration = GetMethodParameters(stackFrame);
            logData.Line = stackFrame.GetFileLineNumber().ToString();
            logData.FileName = stackFrame.GetFileName();
        }

        private static string GetMethodParameters(StackFrame stackFrame)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var parameterInfo in stackFrame.GetMethod().GetParameters())
            {
                sb.Append(parameterInfo.ParameterType);
                sb.Append(" ");
                sb.Append(parameterInfo.Name);
                sb.Append(", ");
            }
            return sb.ToString().TrimEnd(", ".ToCharArray());
        }
    }

}
